 * Begin log file
capture log close sublog
log using "$projdir/log/5_cty_mw_growth.txt", name(sublog) text replace

*-------------------------------------------------------------------------------
* Description: Get a MW growth by county to use when calculating elasticities
* Author: Denis Sosinskiy
*
* Updated: January 26, 2024
*-------------------------------------------------------------------------------
clear

* Create tempfiles
tempfile core
tempfile core0

* Start a loop through samples of interest
foreach samp in e21 e33 mcd_e21 {
	* Set start and end quarters
	local trp = 218
	local start_tm = 199
	local end_tm = 251 
	local tot_tm = `end_tm' - `start_tm' + 1
	local endqtrlab "2019Q4"
	if `end_tm' == 251 {
		local endqtrlab "2022Q4"
	}

	* Load the data
	use "$projdir/dta/build/cln/analysis_panel_cty.dta", clear

	* Rename key variables
	qui rename (quarterly_date earnings lnemp ind) (tm demean_lnearn demean_lnemp naics)
	format tm %tq

	* Generate unique county id and drop obs which don't observe it
	qui gen cty_fips = 1000*statefips + countyfips
	qui keep if !mi(cty_fips) & !mi(avg_wkly_wage) & !mi(employment) & avg_wkly_wage != 0 & employment != 0
	assert cty_fips
	assert avg_wkly_wage
	assert employment
	assert !mi(avg_wkly_wage)
	assert !mi(employment)

	* Recode pre-2011 full- and limited-service restaurant NAICS codes
	qui recode naics (722211=722513) if year <= 2011

	* Keep NAICS  722, and 722513
	qui keep if inlist(naics, 722, 722513)

	* Sort
	sort cty_fips tm naics

	* Identify treated and donor counties
	levelsof cty_fips if inlist(statefips, 6, 36), local(trcty)
	local trctylist = subinstr("`trcty'", " ", ",",.)
	qui distinct cty_fips if inlist(cty_fips, `trctylist')
	local trcty_count = r(ndistinct)

	* Drop donor counties
	qui drop if !inlist(cty_fips, `trctylist')

	* Restrict to treated counties
	qui gen x = (naics == 722 & min_emp >= 5000)
	bysort cty_fips: egen empkeep = max(x)
	qui keep if empkeep == 1
	keep if naics == 722513

	* Save tempfile
	qui compress
	qui save "`core'", replace

	* Make county change adjustments and get mean of avg_wkly_wage over remaining counties
	qui do "$projdir/do/build/cw/cty_changes_NYCnocombine.do"
	qui collapse (mean) avg_wkly_wage year qtr loc_max_mw [aw=employment], by(cty_fips tm naics)

	* Save tempfile
	qui save "`core0'", replace

	* Reload the data
	qui use "`core'", clear

	* Make county change adjustments and get sum of employment over remaining counties
	qui do "$projdir/do/build/cw/cty_changes_NYCnocombine.do"
	qui collapse (sum) employment pop10, by(cty_fips tm naics)

	* Merge
	qui merge 1:1 cty_fips tm naics using "`core0'", nogen norep

	* Restrict to desired time-period
	if "`samp'" == "e21" {
		keep if inlist(tm, 215, 239)
	}
	if "`samp'" == "e33" {
		keep if inlist(tm, 215, 251)
	}
	if "`samp'" == "mcd_e21" {
		keep if inlist(tm, 224, 239)
	}
	
	bysort cty_fips: egen mw_beg = min(loc_max_mw)
	bysort cty_fips: egen mw_end = max(loc_max_mw)
	
	gen pct_mw_growth = ((mw_end - mw_beg)/mw_beg)*100

	collapse (mean) mw_beg mw_end pct_mw_growth, by(cty_fips)

	save "$projdir/dta/build/cln/cty_mw_growth_`samp'", replace
}

* Close the log
log close sublog
